package cn.life.business.role.dao.impl;

import cn.life.business.role.dao.RoleDao;
import cn.life.business.role.entity.po.RolePo;
import cn.life.business.role.entity.vo.RoleVo;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import org.springframework.stereotype.Repository;

/**
 * @Description:    用户角色Dao实现层
 *
 * @Author 李佳明
 * @Date 2018-07-25 08:56
 */
@Repository
public class RoleDaoImpl extends AbstractSupportDao implements RoleDao{

    @Override
    public Long addRole(RolePo rolePo) {
        Long id = queryTableNextId("user_role","role_id");
        String sql = "insert into user_role(role_id,user_id,user_name,project_id,user_role,start_date,end_date,relation_state,opr_user_id,opr_user_name,opr_date) values(?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql,id,rolePo.getUserId(),rolePo.getUserName(),rolePo.getProjectId(),rolePo.getUserRole(),rolePo.getStartDate(),rolePo.getEndDate(),rolePo.getRelationState(),rolePo.getOprUserId(),rolePo.getOprUserName(),getNowDate()) == 1 ? id : null;
    }

    @Override
    public int updateRole(RolePo rolePo) {
        String sql = "update user_role set user_role = ?,start_date = ?,end_date = ?,relation_state = ? where role_id = ?";
        return update(sql,rolePo.getUserRole(),rolePo.getStartDate(),rolePo.getEndDate(),rolePo.getRelationState(),rolePo.getRoleId());
    }

    @Override
    public int deleteRole(RolePo rolePo) {
        String sql = "delete from user_role where role_id = ?";
        return update(sql,rolePo.getRoleId());
    }

    @Override
    public Pagination<RoleVo> queryRoleList(RolePo rolePo) {
        String sql = "select a.*,case a.user_role when 1 then '技术工长' when 2 then '技术监管' when 3 then '库管人员' when 4 then '采购人员' when 5 then '管理人员' when 6 then '文献人员' else '无' end  as roleName,b.project_name as projectName from user_role a inner join project_info b on a.project_id = b.project_id where a.project_id = ? ";
        if(rolePo.getUserName() != null && !rolePo.getUserName().equals("")){
            sql = sql + " and user_name like '%" + rolePo.getUserName() + "%'";
        }
        if(rolePo.getRelationState() != null){
            sql = sql + " and relation_state = " + rolePo.getRelationState() + "";

        }
        sql = sql + " order by opr_date desc";
        return queryMyForListByPageEx(sql,RoleVo.class,rolePo.getPageIndex(),rolePo.getPageSize(),new Object[]{rolePo.getProjectId()});
    }

    @Override
    public RoleVo queryRoleDetail(RolePo rolePo) {
        String sql = "select a.*,case a.user_role when 1 then '技术工长' when 2 then '技术监管' when 3 then '库管人员' when 4 then '采购人员' when 5 then '管理人员' when 6 then '文献人员' else '无' end  as roleName,b.project_name as projectName,b.factory_id as factoryId from user_role a inner join project_info b on a.project_id = b.project_id and relation_state = 1 where a.user_id = ?";
        return queryForObject(sql,RoleVo.class,rolePo.getUserId());
    }
}
